AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

local minCoolant = 5	
local minEnergy = 200
local minNeutrions = 30

function ENT:Initialize()
	self.BaseClass.Initialize(self)
	self.Active = 0
	self.damaged = 0
	self:SetPos((self:GetPos()+ Vector(0,0,3)))
	self.x = 1
	self:SetMoveType(MOVETYPE_NONE);
	self:SetSolid(SOLID_VPHYSICS);

	if not (WireAddon == nil) then
		self.WireDebugName = self.PrintName
		self.Inputs = Wire_CreateInputs(self.Entity, { "On" })
		self.Outputs = Wire_CreateOutputs(self.Entity, { "On", "Output" })
	end
end



function ENT:TurnOn()


	if (self.Active == 0 and RD_GetResourceAmount(self, "coolant") >= minCoolant and RD_GetResourceAmount(self, "energy") >= minEnergy and RD_GetResourceAmount(self, "neutrions") >= minNeutrions) then
		if(owner == 4 or owner == self.teamNo) then
			self.Active = 1
			if not (WireAddon == nil) then 
				Wire_TriggerOutput(self.Entity, "On", 1)
				Wire_TriggerOutput(self.Entity, "Output", 1)
			end
			self:SetOOO(1)
			
		else
			//p:PrintMessage( HUD_PRINTCENTER , "This Space now controlled by:"..All_Teams[ent.teamNo].)
		end
	
	end
end



function ENT:TurnOff()
	if (self.Active == 1) then
		self.Active = 0

		x = 0
		for k, v in pairs( ents.FindInSphere(self.pos, self.radius) ) do
			if(v:GetClass("spawner") and v.teamNo == self.teamNo and v != self and v.Active == 1) then
				x = x+1
			end
		end
			
		if(x<1) then
			Unregister_Planet_Owner(self, self.ply)
		end
		
		if not (WireAddon == nil) then 
			Wire_TriggerOutput(self.Entity, "On", 0)
			Wire_TriggerOutput(self.Entity, "Output", 0)
		end
		self:SetOOO(0)
	end
end



function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		self:SetActive(value)
	end
end



function ENT:Repair()
	self:TurnOff()
end



function ENT:Destruct()
	LS_Destruct( self.Entity, true )
	self:TurnOff()
end



function ENT:Spawner()

	if(RD_GetResourceAmount(self, "coolant") >= minCoolant and RD_GetResourceAmount(self, "energy") >= minEnergy and RD_GetResourceAmount(self, "neutrions") >= minNeutrions)  then
		RD_ConsumeResource(self, "coolant", minCoolant/10)
		RD_ConsumeResource(self, "neutrions", minNeutrions/50) 
		RD_ConsumeResource(self, "energy", minEnergy/50) 
		Register_Planet_Owner(self, self.ply)
	else
		self:TurnOff()
	end

	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Output", tostring(self:GetPos())) end
end



function ENT:OnRemove()
	self.BaseClass.OnRemove(self)
	self:TurnOff()
end



function ENT:Think()
	self.BaseClass.Think(self)
	if (self.Active == 1) then
		self:Spawner()
	
	end
	if(	self.teamNo == nil) then
		self.teamNo = self.ply:Team()
	end
	local valid, planet, pos , radius, gravity, habitat, air, co2, n, atmosphere, pressure, ltemperature, stemperature, sunburn = SB_Get_Environment_Info(self.actualPlanetID)
	self.pos = pos
	self.radius = radius
	owner = SB_Get_Environment_Owner(self.actualPlanetID)
	planetData = SB_Get_Environment_Info(self.actualPlanetID)
	self.Entity:NextThink(CurTime() + 0.001)
	return true
end
